package main

import (
	"io/ioutil"
	"os"

	"gopkg.in/yaml.v2"
)

var C = &Config{ //默认配置
	Prefix:     "auto_FF_",
	AtHour:     0,
	MaxBackups: 7,
	Shutdown:   false,
	Debug:      true,
}

type Config struct {
	Prefix     string `yaml:"prefix"`      //快照命名前缀
	AtHour     int    `yaml:"at_hour"`     //一天的什么时候开始执行快照工作
	MaxBackups int    `yaml:"max_backups"` //快照最多保留多少份
	Shutdown   bool   `yaml:"shutdown"`    //先关闭虚拟机，再执行快照
	Debug      bool   `yaml:"debug"`       //将shell命令的标准输出和错误输出打印到cmdOut.txt文件
}

//将全局变量 *C 写入配置文件
func (c *Config) Save(file string) error {
	out, err := yaml.Marshal(C)
	if err != nil {
		return err
	}

	return ioutil.WriteFile(file, out, os.ModePerm)
}

//加载配置文件到全局变量 *C
func (c *Config) Load(file string) error {
	d, err := ioutil.ReadFile(file)
	if err != nil {
		return err
	}

	return yaml.Unmarshal(d, C)
}
